/*
 *  This file is part of Spidex.
 *  
 *  Spidex is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *  
 *  Spidex is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with Spidex.  If not, see <http://www.gnu.org/licenses/>.
 */ 
package spidex;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;


// <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
import spidex.dbmanager.MediaType;
// #[regen=yes,id=DCE.570A699C-A7BE-AAE0-C7B7-6D8FD7FD8785]
import spidex.importer.ColumnType;
// </editor-fold> 
public class DVDInfo implements FilmInfoInterface {
    private int id = 0;
    private int rating = 0;
    private boolean borrowed = false;
    private boolean rented = false;
    private String rentedToWho;
    private String borrowedFromWho;
    private MediaType type = MediaType.DVD;
    private String compilation;
    private String info;
    private FilmInfoInterface filmInfo;
    private static Pattern pattern = Pattern.compile("(\\d+)(.*)");
    private Matcher matcher;

    public DVDInfo (FilmInfoInterface info) {
        if(info == null) {
            throw new NullPointerException("info");
        }
        this.filmInfo = info;       
    }
    
    public DVDInfo () {
        this.filmInfo = new FilmInfo();       
    }
    
    public MediaType getType() {
        return this.type;
    }
    
    public void setType(MediaType val) {
        this.type = val;
    }
    
    public void setType(int val) {
        this.type = MediaType.getType(val);
    }
    
    public String getCompilation() {
        return this.compilation;
    }
    
    public void setCompilation(String val) {
        this.compilation = val;
    }
    
    public String getInfo() {
        return this.info;
    }
    
    public void setInfo(String val) {
        this.info = val;
    }    

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.62BE5B90-3ABC-E04B-39D9-B8B3470F04B3]
    // </editor-fold> 
    public String getTitle () {
        return filmInfo.getTitle();
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.D0247609-83B3-72FC-DC3A-1A98AA8DCBCE]
    // </editor-fold> 
    public void setTitle (String val) {
        filmInfo.setTitle(val);
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.6FA6ABD9-2CC4-FC9D-1ECF-27B1780E188E]
    // </editor-fold> 
    public Set<String> getDirectors () {
        return filmInfo.getDirectors();
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.CF2B46FB-70AB-6E50-250C-66D088B991E5]
    // </editor-fold> 
    public void addDirector (String director) {
        filmInfo.addDirector(director);
    }
    
    public String toStringDirectors() {
        String result = "";
        boolean first = true;
        for(String director : this.getDirectors()) {
            if(first) {
                result += director;
                first = false;
            }
            else {
                result += ", "+director;
            }
        }
        return result;
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.CE96774F-9706-6AB2-39FC-C786C882ADBD]
    // </editor-fold> 
    public Set<String> getActors () {
        return this.filmInfo.getActors();
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.6BAF3BE5-713F-5E82-EAB1-060DA910569D]
    // </editor-fold> 
    public void addActor (String actor) {
        filmInfo.addActor(actor);
    }
    
    public String toStringActors() {
        String result = "";
        boolean first = true;
        for(String actor : this.getActors()) {
            if(first) {
                result += actor;
                first = false;
            }
            else {
                result += ", "+actor;
            }
        }
        return result;
    }
    
    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.C1455DC8-C50A-BADD-C271-2DA1A5F54CA0]
    // </editor-fold> 
    public String getGenre () {
        return this.filmInfo.getGenre();
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.00D0E5E8-8909-9E99-9E32-5E0B2901104A]
    // </editor-fold> 
    public void setGenre (String val) {
        this.filmInfo.setGenre(val);
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.F93E2060-002D-E920-AEE1-C668B53447A7]
    // </editor-fold> 
    public int getYearOfRelease () {
        return this.filmInfo.getYearOfRelease();
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.93C8063F-2F9D-DAB7-7B06-24DC7E06A372]
    // </editor-fold> 
    public void setYearOfRelease (int val) {
        this.filmInfo.setYearOfRelease(val);
    }
    
    public void setYearOfRelease (String val) {
        try {
            this.filmInfo.setYearOfRelease( Integer.parseInt(val));
        }
        catch(NumberFormatException ex) {
            this.filmInfo.setYearOfRelease( 0 );
        }
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.56F0ADCD-F324-09D8-2620-6E11A5802BB0]
    // </editor-fold> 
    public int getLength () {
        return filmInfo.getLength();
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.BFEF57CC-FB7D-EAF4-E687-8EBA43E25284]
    // </editor-fold> 
    public void setLength (int val) {
        filmInfo.setLength(val);
    }

    public void setLength (String val) {
        matcher = pattern.matcher(val);
        if(matcher.matches()) {
            try {
                this.filmInfo.setLength( Integer.parseInt( matcher.group(1) ) );
            }
            catch(IllegalStateException ex) {
                this.filmInfo.setLength( 0 );
            }
        }
        else {
            this.filmInfo.setLength( 0 );
        }
    }    
    
    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.887FA557-338B-0B28-BC91-89DA51B9E6CE]
    // </editor-fold> 
    public ImageIcon getPoster () {
        return filmInfo.getPoster();
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.054DF74B-F813-B061-4833-C634AAE64260]
    // </editor-fold> 
    public void setPoster (ImageIcon val) {
        this.filmInfo.setPoster(val);
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.16478973-26D5-8129-C0FA-94142223BE71]
    // </editor-fold> 
    public boolean getBorrowed () {
        return this.borrowed;
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.CA938C19-E565-6FDF-6C7C-1D063A2CC5AC]
    // </editor-fold> 
    public void setBorrowed (boolean val) {
        this.borrowed = val;
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.B8BC6EEF-7BF5-5379-B365-A68AD4A25BC5]
    // </editor-fold> 
    public String getBorrowedFromWho () {
        return borrowedFromWho;
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.1E69EDF2-0963-2B2D-05B5-BCA5D6225E38]
    // </editor-fold> 
    public void setBorrowedFromWho (String val) {
        if(val == null || val.isEmpty()) {
            this.borrowed = false;
            borrowedFromWho = val;
            return;
        }
        
        this.borrowed = true;
        borrowedFromWho = val;
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.7F6A92B0-32DB-08A4-78A2-45674C4F5674]
    // </editor-fold> 
    public int getId () {
        return id;
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.99663834-95DA-9659-D913-EF4F186CCF4D]
    // </editor-fold> 
    public void setId (int val) {
        this.id = val;
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.151E3C07-EA90-33BD-863A-51ECAB12A1FE]
    // </editor-fold> 
    public int getRating () {
        return rating;
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.7B9D5F11-C54F-B473-A1A3-5D9E7F7D1CA7]
    // </editor-fold> 
    public void setRating (int rating) {
        if(rating < 0 || rating > 10) {
            throw new IllegalArgumentException("rating");
        }
        this.rating = rating;
    }
    
    public void setRating (String val) {
        int intRating;
        try {
            intRating = Integer.parseInt(val);
        }
        catch(NumberFormatException ex) {
            intRating = 0;
        }
        if(intRating < 0 || intRating > 10) {
            throw new IllegalArgumentException("rating");
        }
        this.rating = intRating;
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.822D30F7-9EC2-0089-ED6E-EA06647498E5]
    // </editor-fold> 
    public boolean getRented () {
        return rented;
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.8DEABFBC-78BA-4EC1-6BEA-5AC2BDAA9F17]
    // </editor-fold> 
    public void setRented (boolean val) {
        rented = val;
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.A19E23E6-34F2-CE11-A924-D636954A874E]
    // </editor-fold> 
    public String getRentedToWho () {
        return rentedToWho;
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.4C164950-49C3-B1B0-EB9A-4D8FBD571B9C]
    // </editor-fold> 
    public void setRentedToWho (String val) {
        if(val == null || val.isEmpty()) {
            this.rented = false;
            rentedToWho = val;
            return;
        }
        this.rented = true;
        rentedToWho = val;
    }
    
    public String getFilmUrl() {
        return this.filmInfo.getFilmUrl();
    }

    public void setFilmUrl(String filmUrl) {
        this.filmInfo.setFilmUrl(filmUrl);
    }

    public String getPosterUrl() {
        return this.filmInfo.getPosterUrl();
    }

    public void setPosterUrl(String posterUrl) {
        this.filmInfo.setPosterUrl(posterUrl);
    }

    @Override
    public String toString () {
        return ""+getId()+"|Name:"+getTitle()+"|Year:"+getYearOfRelease()+
                "|"+getRating()+"|"+getLength()+"|"+getType()+"|"+getGenre()+
                "|"+getActors()+"|"+getDirectors()+
                "|Borrowed to:"+getBorrowedFromWho()+"|Lend from:"+getRentedToWho()+
                "|Compilation:"+getCompilation()+"|"+getType();
    }
    
    public void savePoster(String filePath) throws IOException {
        if(this.getPoster() == null) {
            throw new NullPointerException("poster");
        }
        if(filePath == null) {
            throw new NullPointerException("filePath");
        }
        
        File file = new File(filePath);
        System.out.println("Saving to "+filePath);
        
        int width = this.getPoster().getIconWidth();
        int height = this.getPoster().getIconHeight();
        BufferedImage buff = new BufferedImage(
            width,height,BufferedImage.TYPE_INT_RGB
        );
        Graphics2D imageGraphics = buff.createGraphics();
        imageGraphics.drawImage(this.getPoster().getImage(), 0, 0, width, height, null);
        ImageIO.write(buff, "jpg", new FileOutputStream( file ) );
    }
    
    public FilmInfoInterface getFilmInfo() {
        return this.filmInfo;
    }
    
    public void setFilmInfo( FilmInfoInterface info) {
        this.filmInfo = info;
    }
    
    public String getValue(ColumnType columnType) {
        switch(columnType.getInt()) {
            case 1: return this.getTitle(); 
            case 2: return this.getCompilation();
            case 3: return ""+this.getRating();
            case 4: return this.getRentedToWho();
            case 5: return this.getBorrowedFromWho();
            case 6: return this.getInfo(); 
            case 7: return this.getGenre();
        }
        return "";
    }
    
    public void setValue(ColumnType columnType, String value) {
        if(columnType == null) {
            return;
        }
        
        switch(columnType.getInt()) {
            case 1: setTitle(value); break;
            case 2: setCompilation(value); break;
            case 3: setRating(value); break;
            case 4: setRentedToWho(value); break;
            case 5: setBorrowedFromWho(value); break;
            case 6: setInfo(value); break;
            case 7: setGenre(value); break;
        }
    }
    
    @Override
    public DVDInfo clone() {
        DVDInfo newInfo = new DVDInfo(filmInfo.clone());
            newInfo.setId(id);
            newInfo.setRating(rating);
            newInfo.setBorrowed(borrowed);
            newInfo.setRented(rented);
            newInfo.setRentedToWho(rentedToWho);
            newInfo.setBorrowedFromWho( borrowedFromWho );
            newInfo.setType(type);
            newInfo.setCompilation(compilation);
            newInfo.setInfo(info);
        return newInfo;
    }
    
    public static void main(String args[]) throws IOException {
        DVDInfo info = new DVDInfo();
        info.setLength("136min");
//        info.savePoster("/home/xatrix/poster");
    }
    
}

